<?php

namespace App\Http\Controllers\Admin\SuperAdmin;

use App\Services\Admin\SuperAdmin\ApiService;
use Exception;
use Illuminate\Http\JsonResponse;

class ApiController extends BaseSuperAdminController
{

    protected ApiService $apiService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->apiService = $this->loadService('Admin\SuperAdmin\ApiService');
    }

    /**
     * 创建API
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function createApi(): JsonResponse
    {
        $rule = [
            'path' => 'required|string|min:1',
            'apiGroup' => 'required|string|min:1',
            'method' => 'required|string|min:1',
            'description' => 'required|string|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->apiService->createApi($this->params);
        return outputJsonResult();
    }

    /**
     * 删除API
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function deleteApi(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->apiService->deleteApi($this->params);
        return outputJsonResult();
    }

    /**
     * 更新API
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function updateApi(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'path' => 'required|string|min:1',
            'apiGroup' => 'required|string|min:1',
            'method' => 'required|string|min:1',
            'description' => 'required|string|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->apiService->updateApi($this->params);
        return outputJsonResult();
    }

    /**
     * 更新角色API
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function updateAuthorityApi(): JsonResponse
    {
        $rule = [
            'authorityId' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->apiService->updateAuthorityApi($this->params);

        return outputJsonResult();
    }

    /**
     * 批量删除
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function deleteApisByIds(): JsonResponse
    {
        $rule = [
            'ids' => 'required|array',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->apiService->deleteApisByIds($this->params);

        return outputJsonResult();
    }

    /**
     * 获取api列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getApiList(): JsonResponse
    {
        $list = $this->apiService->getApiList($this->params);
        return outputJsonResult($list);
    }

    /**
     * 所有api列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function allApiList(): JsonResponse
    {
        $list = $this->apiService->allApiList();
        return outputJsonResult($list);
    }

    /**
     * 获取权限列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function getPolicyPathByAuthorityId(): JsonResponse
    {

        $rule = [
            'authorityId' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $list = $this->apiService->getPolicyPathByAuthorityId($this->params);
        return outputJsonResult($list);
    }

    /**
     * 详情
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->apiService->detail($this->params);
        return outputJsonResult($detail);
    }

}
